﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.BaseService.Contract.Studio;
using DWF.Studio.BaseService.Contract.Studio.Models;
using DWF.Studio.Custom;
using DWF.Studio.Project;
using DWF.Studio.Share.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DWF.Studio.BaseService
{
    /// <summary>
    /// 设计器服务实现
    /// </summary>
    internal class DesignerService : IDesignerService
    {
        private ProjectManager _projectManager = null;

        public DesignerService()
        {
            _projectManager = ProjectManager.Instance;
        }

        /// <summary>
        /// 在当前打开工作流设计器中，添加工作流节点。
        /// </summary>
        /// <param name="parentDisplayName"></param>
        /// <param name="activityTypeName"></param>
        /// <param name="param"></param>
        public IEnumerable<ActivityInfo> AddActivityToWorkflow(string parentCode, string activityTypeName, SerializableParameters param)
        {
            var json = GlobalActionHandler.AddActivityToWorkflow?.Invoke(parentCode, activityTypeName, param.ToDictionary());
            var result = JsonHelper.Deserialize<List<ActivityInfo>>(json);

            return result;
        }

        public void CloseFile(string fileFullName)
        {
            throw new NotImplementedException();
        }

        public string GetRootActivityCode()
        {
            return GlobalActionHandler.GetRootActivityCode?.Invoke();
        }

        public string GetRootActivityTypeName()
        {
            return GlobalActionHandler.GetRootActivityTypeName?.Invoke();
        }

        public void SetActivityByCode(string activityCode, SerializableParameters param)
        {
            GlobalActionHandler.SetActivityByCode?.Invoke(activityCode, param.ToDictionary());
        }

        public void ShowFile(string filePath)
        {
            var file = _projectManager.GetProjectFile(filePath);
            if (file != null)
            {
                GlobalActionHandler.ScriptShow?.Invoke(new TagEventArgs<Project.ProjectFile>(file));
            }
        }
    }
}
